package com.yxinsur.imc.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxinsur.imc.business.domain.ImcOrder;
import com.yxinsur.imc.business.enums.OrderPersonTypeEnum;
import com.yxinsur.imc.business.mapper.ImcOrderBankInfoEndorseMapper;
import com.yxinsur.imc.business.mapper.ImcOrderMapper;
import com.yxinsur.imc.business.mapper.ImcOrderPersonEndorseMapper;
import com.yxinsur.imc.business.mapper.ImcOrderRiskEndorseMapper;
import com.yxinsur.imc.business.service.IImcOrderEndorseService;
import com.yxinsur.imc.business.vo.*;
import com.yxinsur.imc.common.utils.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 批单Service业务层处理
 *
 * @author imc
 * @date 2021-03-18
 */
@Service
public class ImcOrderEndorseServiceImpl extends ServiceImpl<ImcOrderMapper, ImcOrder> implements IImcOrderEndorseService {
    private static final Logger logger = LoggerFactory.getLogger(ImcOrderEndorseServiceImpl.class);
    @Autowired
    private ImcOrderMapper imcOrderMapper;
    @Autowired
    private ImcOrderRiskEndorseMapper imcOrderRiskEndorseMapper;
    @Autowired
    private ImcOrderPersonEndorseMapper imcOrderPersonEndorseMapper;
    @Autowired
    private ImcOrderBankInfoEndorseMapper imcOrderBankInfoEndorseMapper;

    /**
     * 根据{保单ID}查询批单详情
     *
     * @param orderId 保单ID
     * @return 批单详情
     */
    @Override
    public OrderEndorseVo getOrderEndorseByOrderId(long orderId) {
        long tenantId = SecurityUtils.getTenantId();
        OrderEndorseVo orderEndorseVo = new OrderEndorseVo();
        // 基本信息
        ImcOrder imcOrder = this.imcOrderMapper.selectById(orderId);
        OrderVo orderVo = new OrderVo();
        BeanUtil.copyProperties(imcOrder, orderVo, CopyOptions.create().ignoreCase().setIgnoreNullValue(true));
        orderEndorseVo.setOrderVo(orderVo);

        // 险种信息
        List<OrderRiskEndorseVo> riskEndorseVoList = this.imcOrderRiskEndorseMapper.listOrderRiskEndorseVoByOrderId(orderId, tenantId);
        orderEndorseVo.setRiskEndorseVoList(riskEndorseVoList);

        // 人员信息
        List<OrderPersonEndorseVo> applicantVoList = this.imcOrderPersonEndorseMapper.listOrderPersonEndorseVoByOrderId(orderId, tenantId, OrderPersonTypeEnum.A.getCode());
        List<OrderPersonEndorseVo> insurantVoList = this.imcOrderPersonEndorseMapper.listOrderPersonEndorseVoByOrderId(orderId, tenantId, OrderPersonTypeEnum.B.getCode());
        List<OrderPersonEndorseVo> favourVoList = this.imcOrderPersonEndorseMapper.listOrderPersonEndorseVoByOrderId(orderId, tenantId, OrderPersonTypeEnum.C.getCode());
        orderEndorseVo.setApplicantEndorseVoList(applicantVoList);
        orderEndorseVo.setInsurantEndorseVoList(insurantVoList);
        orderEndorseVo.setFavourEndorseVoList(favourVoList);

        // 缴费信息
        List<OrderBankInfoEndorseVo> bankInfoEndorseVoList = this.imcOrderBankInfoEndorseMapper.listOrderBankInfoEndorseVoByOrderId(orderId, tenantId);
        orderEndorseVo.setBankInfoEndorseVoList(bankInfoEndorseVoList);
        return orderEndorseVo;
    }
}
